package nagel_schreckenberg;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Panel extends JPanel{
	
	private ArrayList<Ellipse2D> vehicles;
	private Environment env;
	private double str = 5;
	
	public Panel(int w, int h){
		setPreferredSize(new Dimension(w, h));
	}

	public void updateGraphics(Environment e){
		vehicles = new ArrayList<Ellipse2D>();
		env = e;
		int x; int y;
		
		for(Vehicle v : e.getLanes().get(0).getVehicles()){
			x = 190;
			y = 50 + v.getPosition();
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		for(Vehicle v : e.getLanes().get(4).getVehicles()){
			x = 210;
			y = 150 - v.getPosition();
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		
		for(Vehicle v : e.getLanes().get(1).getVehicles()){
			x = 320 - v.getPosition();
			y = 160;
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		for(Vehicle v : e.getLanes().get(5).getVehicles()){
			x = 220 + v.getPosition();
			y = 180;
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		
		for(Vehicle v : e.getLanes().get(2).getVehicles()){
			x = 210;
			y = 290 - v.getPosition();
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		for(Vehicle v : e.getLanes().get(6).getVehicles()){
			x = 190;
			y = 190 + v.getPosition();
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		
		for(Vehicle v : e.getLanes().get(3).getVehicles()){
			x = 80 + v.getPosition();
			y = 180;
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
		for(Vehicle v : e.getLanes().get(7).getVehicles()){
			x = 180 - v.getPosition();
			y = 160;
			vehicles.add(new Ellipse2D.Double(x-str/2, y-str/2, str, str));
		}
	}

	public void updateInformation(double d){
		
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	    Graphics2D g2 = (Graphics2D)g;
	    g2.setPaint(Color.black);
	    try{
	    for(Ellipse2D e : vehicles){
	    	g2.draw(e);
	    }
	    
	    g2.drawRect(180, 150, 40, 40);
	    g2.drawLine(190, 50, 190, 150);
	    g2.drawLine(210, 50, 210, 150);
	    g2.drawLine(220, 160, 320, 160);
	    g2.drawLine(220, 180, 320, 180);
	    g2.drawLine(210, 190, 210, 290);
	    g2.drawLine(190, 190, 190, 290);
	    g2.drawLine(80, 180, 180, 180);
	    g2.drawLine(80, 160, 180, 160);
	    Ellipse2D n = new Ellipse2D.Double(185, 145, str, str);
	    if(env.getLanes().get(0).getPhase()){g2.setPaint(Color.green);}else{g2.setPaint(Color.red);}
	    g2.fill(n);
	    g2.draw(n);
	    Ellipse2D e = new Ellipse2D.Double(220, 155, str, str);
	    if(env.getLanes().get(1).getPhase()){g2.setPaint(Color.green);}else{g2.setPaint(Color.red);}
	    g2.fill(e);
	    g2.draw(e);
	    Ellipse2D s = new Ellipse2D.Double(210, 190, str, str);
	    if(env.getLanes().get(2).getPhase()){g2.setPaint(Color.green);}else{g2.setPaint(Color.red);}
	    g2.fill(s);
	    g2.draw(s);
	    Ellipse2D w = new Ellipse2D.Double(175, 180, str, str);
	    if(env.getLanes().get(3).getPhase()){g2.setPaint(Color.green);}else{g2.setPaint(Color.red);}
	    g2.fill(w);
	    g2.draw(w);
	    }
	    catch(Exception e){System.out.println("no graphics available");}
	}
}
